import * as React from 'react';
import { createStackNavigator,StackNavigator } from '@react-navigation/stack';
import { View, Text,ScrollView,ImageBackground ,StyleSheet,
  Dimensions,TextInput,TouchableOpacity,AsyncStorage,DeviceEventEmitter} from 'react-native';
import Textarea from 'react-native-textarea';
import Icon from 'react-native-vector-icons/AntDesign';
import Btn from '../../common/Btn';
import '../../common/global';

const Newlist = ({ navigation,route })=>{
  const [context, onChangecon] = React.useState('');

  // const storeData=async()=>{
  //   let data =await AsyncStorage.getItem('data');
  //   var timestamp = Date.parse(new Date());
  //   var date = new Date(timestamp);
  //   var time=date.getHours()+':' +date.getMinutes();
  //   data =data?JSON.parse(data):[]
  //   data.unshift({context:context,time:time})
  //   await AsyncStorage.setItem('data',JSON.stringify(data))
  //   // navigation.pop('word',{data:{}})
  //   navigation.push('TreeHole',{data:{}})
  // }


  const save=async()=>{
    let data =await AsyncStorage.getItem('data');
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    var month =date.getMonth()+1;
    var time=date.getFullYear()+'年'+month+'月'+date.getDate()+'日'+date.getHours()+':' +date.getMinutes();
    data =data?JSON.parse(data):[]
    data.unshift({context:context,time:time})
    await AsyncStorage.setItem('data',JSON.stringify(data))
    DeviceEventEmitter.emit('refresh',{
          'a':data,
        })
    
    // AsyncStorage.clear()
    // let data =AsyncStorage.getItem('data');
    // var localdata =data?JSON.parse(data):[]
    // var object = {
    //     context:context,
    //     data:date.getHours()+':' +date.getMinutes()
    // };
    // data.unshift({context:context,time:time})
    console.log(data)
    navigation.goBack();
  //   new Promise((resolve, reject) => {
  //   AsyncStorage.setItem('localdata',JSON.stringify(localdata), (error) => {
  //       if (error) {
  //           // 错误
  //       } else {
  //           // 存储成功
  //           resolve(true);
  //       }
  //   })
  // }).then(() => {
  //    console.log('back');
  //    navigation.goBack();
  //   DeviceEventEmitter.emit('refresh',{
  //     'a':localdata,
  //   })
  //  })
  }
  return (
    <ScrollView>
      <View>
        <ImageBackground style={styles.tu}
        source={require('./tuu.jpg')}>
          <TouchableOpacity style={styles.goback} onPress={()=>{navigation.goBack()}}>
            <Icon size={40} name='left' color='#DCDCDC'></Icon></TouchableOpacity>
          <Textarea placeholder='别怕~释放自己的情绪吧&#13;&#10;这里只有树洞菌在守护' 
          style={styles.input} onChangeText={text=>onChangecon(text)}
          value={context}></Textarea>
          <TouchableOpacity style={styles.delete} onPress={()=>{navigation.replace('Newlist')}}>
            <Icon size={40} name='closecircleo' color='#808080'></Icon>
            <Text>化菌</Text></TouchableOpacity>
          <TouchableOpacity style={styles.save} onPress={save}>
            <Icon size={40} name='checkcircleo' color='#808080'></Icon>
            <Text>存好</Text></TouchableOpacity>
        </ImageBackground>
      </View>
    </ScrollView>
  )
}
export default Newlist;


const styles = StyleSheet.create({
  tu:{
    position: 'relative',
    flex:1,
    width: ptd(375),
    marginLeft:ptd(0),
    marginRight:ptd(0),
    height:Dimensions.get('window').height,
    
  },
  goback:{
    height:60,
    width:80,
    paddingLeft:ptd(10),
    paddingTop:10, 
    // backgroundColor:'pink'   
  },
  input:{
    textAlign:'center',
    fontSize:20,
    height:200,
    marginTop:0.35*Dimensions.get('window').height,
    color:'#A0522D'
    // borderColor:'pink',
    // borderWidth:2,
  },

  delete:{
    position: 'absolute',
    // backgroundColor:'pink',
    width:80,
    height:60,
    marginLeft:ptd(40),
    alignItems: 'center', 
    top: 0.83*Dimensions.get('window').height,
  },
  save:{
    position: 'absolute',
    // backgroundColor:'pink',
    width:80,
    height:60,
    marginLeft:ptd(295),
    alignItems: 'center', 
    top: 0.84*Dimensions.get('window').height   
  },

})
